package lee.vioson.ebook.ui.components

import android.annotation.SuppressLint
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
import lee.vioson.ebook.R
import lee.vioson.ebook.ui.AppTheme
import lee.vioson.ebook.viewmodels.PurchaseHistoryViewModel
import lee.vioson.remotedata.bean.PurchaseHistory

/**
 *Author:viosonlee
 *Date:2023/6/12
 *DESCRIPTION:
 */
@Composable
fun PurchaseHistory(
    navHostController: NavHostController,
    viewModel: PurchaseHistoryViewModel = androidx.lifecycle.viewmodel.compose.viewModel()
) {
    val list by viewModel.list.collectAsState()
    Scaffold(
        modifier = Modifier.fillMaxSize(),
        topBar = {
            TopBar(
                title = stringResource(id = R.string.Purchase_History),
                navHostController = navHostController
            )
        }
    ) { paddingValues ->
        LazyColumn(
            modifier = Modifier.padding(paddingValues), content = {
                items(list) {
                    PurchaseHistoryItem(history = it)
                }
            })
    }
}

@Composable
fun PurchaseHistoryItem(history: PurchaseHistory) {
    Column(Modifier.fillMaxWidth()) {
        Row(
            Modifier
                .fillMaxWidth()
                .height(56.dp)
                .padding(horizontal = 16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(text = history.timeStr, fontSize = 14.sp, color = AppTheme.appColors.whiteText80)
            Spacer(modifier = Modifier.weight(1f))
            Text(text = history.type, fontSize = 14.sp, color = AppTheme.appColors.whiteText80)
            Spacer(modifier = Modifier.width(6.dp))
            Text(
                text = "$ ${history.amount}",
                fontSize = 18.sp,
                color = AppTheme.appColors.amountTextColor
            )
        }
        Divider()
    }
}

@OptIn(ExperimentalAnimationApi::class)
@Preview
@Composable
fun PurchaseHistoryPreview() {
    val navHostController = rememberAnimatedNavController()
    PurchaseHistory(navHostController)
}